package com.boot.web.action;

import java.util.LinkedList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.boot.beans.Question;
import com.boot.beans.QuestionItem;
import com.boot.common.BaseAction;
import com.boot.web.service.QuestionItemService;
import com.boot.web.service.QuestionService;

import util.Constant;
import util.FieldUtil;
import util.MapUtil;
import util.MessageUtil;
import util.Page;

@Controller
@RequestMapping("/sys")
public class QuestionAction extends BaseAction {
	@Autowired
	private QuestionItemService	questionItemService;
	private String							actionname	= "考题";
	private String							actionclass	= "Question";
	@Autowired
	private QuestionService			service;

	@RequestMapping(value = "/add2Question.do")
	public String add2() {
		putRequestValue("list1", questionItemService.selectAll(QuestionItem.class, MapUtil.createMap("kemusel", "科目一")));
		putRequestValue("list4", questionItemService.selectAll(QuestionItem.class, MapUtil.createMap("kemusel", "科目四")));
		request.setAttribute("actionname", actionname);
		request.setAttribute("actionclass", actionclass);
		return "addQuestion";
	}

	@RequestMapping(value = "/getQuestion.do")
	public String get(int uid) {
		try {
			putRequestValue("list1", questionItemService.selectAll(QuestionItem.class, MapUtil.createMap("kemusel", "科目一")));
			putRequestValue("list4", questionItemService.selectAll(QuestionItem.class, MapUtil.createMap("kemusel", "科目四")));
			Question bean = service.get(uid);
			request.setAttribute("modifybean", bean);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "modifyQuestion";
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "获取信息失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/addQuestion.do")
	public String add(Question bean) {
		try {

			if (bean.getCoursesel().equals("科目一")) {
				bean.setScore(1);
				bean.setQuestionItemId(getInt("questionItemId1"));
			}
			if (bean.getCoursesel().equals("科目四")) {
				bean.setScore(2);
				bean.setQuestionItemId(getInt("questionItemId4"));
			}
			if (bean.getQuestionItemId() == 0) {
				MessageUtil.addMessage(request, "添加失败.请选择专项");
				return ERROR;
			}
			bean.setZheng(0);
			bean.setCuo(0);
			bean.setAnswer(bean.getAnswer().toUpperCase());
			service.add(bean);
			MessageUtil.addMessage(request, "添加成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "添加失败.");
			return ERROR;
		}

	}

	@RequestMapping(value = "/updateQuestion.do")
	public String update(Question bean) {

		if (bean.getCoursesel().equals("科目一")) {
			bean.setScore(1);
			bean.setQuestionItemId(getInt("questionItemId1"));
		}
		if (bean.getCoursesel().equals("科目四")) {
			bean.setScore(2);
			bean.setQuestionItemId(getInt("questionItemId4"));
		}

		if (bean.getQuestionItemId() == 0) {
			MessageUtil.addMessage(request, "添加失败.请选择专项");
			return ERROR;
		}

		try {
			bean.setAnswer(bean.getAnswer().toUpperCase());
			service.update(bean);
			MessageUtil.addMessage(request, "更新成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "更新失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/queryQuestion.do")
	public String query() {
		try {
			// 字段名称集合
			LinkedList<String> parmnames = new LinkedList<String>();
			// 字段值集合
			LinkedList<Object> parmvalues = new LinkedList<Object>();
			Page p = FieldUtil.createPage(request, Question.class, parmnames, parmvalues);

			Page page = service.selectPage(p, Question.class);
			session.setAttribute(Constant.SESSION_PAGE, page);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "listQuestion";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	@RequestMapping(value = "/deleteQuestion.do")
	public String delete(String ids) {
		try {
			service.delete(ids);
			MessageUtil.addRelMessage(request, "删除信息成功.", "mainquery");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "删除信息失败.");
			return ERROR;
		}
	}

}
